//
//  PickUpMoRecordViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/8.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "PickUpMoRecordViewController.h"
#import "PickMoRecordTableViewCell.h"

@interface PickUpMoRecordViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_listArr;
    NSInteger _pageNumber;
}
@property (strong, nonatomic) UIView *footerView;
@property (strong, nonatomic) UITableView * tableView;
@end

@implementation PickUpMoRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _listArr = [NSMutableArray array];
    _pageNumber = 1;
    if ([self.type isEqualToString:@"pick"]) {
        self.titleLabel.text = @"提现记录";
        [self downloadDataPick];
    }else{
        self.titleLabel.text = @"转账记录";
        [self downloadDataTrans];
    }
    
    [self createFooterView];
    [self createTableView];
}

- (void)downloadDataPick
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MinePickUpRecord) Parameters:@{@"userToken":[Utils getUserToken], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"10"} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
            if ([responseObject[@"data"] count] != 0) {
                [_listArr addObjectsFromArray:responseObject[@"data"]];
            }else{
                [self.view Message:@"无更多数据"];
            }
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
    } andFalse:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [self.view removeAnyView];
    }];
}

- (void)downloadDataTrans
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineTransformRecord) Parameters:@{@"userToken":[Utils getUserToken], @"page":[NSNumber numberWithInteger:_pageNumber], @"page_size":@"10"} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
            if ([responseObject[@"data"] count] != 0) {
                [_listArr addObjectsFromArray:responseObject[@"data"]];
            }else{
                [self.view Message:@"无更多数据"];
            }
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
        }else{
            [self.view Message:responseObject[@"msg"]];
        }
    } andFalse:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [self.view removeAnyView];
    }];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, WIDTH, HEIGHT-70)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"PickMoRecordTableViewCell" bundle:nil] forCellReuseIdentifier:@"PickMoRecordTableViewCell"];
    [_tableView setTableFooterView:_footerView];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNumber = 1;
        [_listArr removeAllObjects];
        if ([self.type isEqualToString:@"pick"]) {
            [self downloadDataPick];
        }else{
            [self downloadDataTrans];
        }
        
    }];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pageNumber ++;
        if ([self.type isEqualToString:@"pick"]) {
            [self downloadDataPick];
        }else{
            [self downloadDataTrans];
        }
        
    }];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PickMoRecordTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"PickMoRecordTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[PickMoRecordTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"PickMoRecordTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellConfigureDic:_listArr[indexPath.row]];
    if ([self.type isEqualToString:@"pick"]) {
        cell.typeLabel.text = @"提现成功";
    }else{
        cell.typeLabel.text = @"转账成功";
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
